package com.why.cachecloud.client.redis.config;

import com.why.cachecloud.client.redis.builder.ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.PipelineCluster;

@Configuration
public class RedisConfiguration {

    /**
     * Redis Cluster
     */
    @Bean(destroyMethod = "close")
    @ConditionalOnProperty(name = "why.components.cachecloud.model", havingValue = "cluster")
    @DependsOn("cacheCloudConstants")
    public PipelineCluster pipelineCluster(@Value("${why.components.cachecloud.appId}") long appId) {
        //默认配置
        PipelineCluster pipelineCluster = ClientBuilder.redisCluster(appId).build();
        return pipelineCluster;
    }

    /**
     * Redis Sentinel
     */
    @Bean(destroyMethod = "destroy")
    @ConditionalOnProperty(name = "why.components.cachecloud.model", havingValue = "sentinel")
    @DependsOn("cacheCloudConstants")
    public JedisSentinelPool jedisSentinelPool(@Value("${why.components.cachecloud.appId}") long appId) {
        //默认配置
        JedisSentinelPool jedisSentinelPool = ClientBuilder.redisSentinel(appId).build();
        return jedisSentinelPool;
    }
}